<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title></title>
        
        <?php include "jquery.php" ?>

    </head>
    <!--  The javascript function disabling textboxes is called on page load -->
    <body onload=viewCustomFields(false);>
        <form name="form" method="post" action="">

            <?php
            //Author: Aya El Mobayad
            //includes the file functions.php in the page in order to access the standardized
            //functions and call them as needed
            include 'functions.php';

            //retrieves the conference id from the associative array using a get request
            $confID = $_GET['conference_id'];

            //specifies the connection to the server, as well as the username and password, and opens it
            $con = mysql_connect('localhost', 'root', '');

            //if the connection wasnt opened, an sql error is returned
            if (!$con) {

                ('Could not connect:' . mysql_error());
            }

            //the database being used is selected from the open connection
            mysql_select_db("mydb", $con);

            //The conference long name is retrieved by calling the get_conference_name function from
            //functions.php and passing it the conference id of the conference for which the name
            //is needed, as a parameter.
            $confName = get_conference_name($confID);

            //The name retrieved using the previous function is printed at the top of the page
            //in a heading element
            print "<h1>{$confName}</h1>";

            //a query that retrieves all the rating criteria from the rating_criteria table in
            //the database is executed and the results are stored in a variable. If the query
            //fails to execute an error message is returned
            $result = mysql_query("SELECT * FROM rating_criteria")
                    or die(mysql_error());

            //a variable num, which stores the number of rows returned from the query is initialized
            $num = mysql_num_rows($result);

            //a condition is made to check the value of the num variable. If no rows were returned,
            //its value should be zero
            if ($num <= 0) {
                //If no rows are returned, a failure message is echoed
                echo '<p align="center">There are no rating crtieria to be added</p>';
            } else {
                //if at least one row was returned, the title is printed
                echo '<br>';
                echo '<h2>Select Rating Criteria: </h2>';
                echo '<br>';
                echo '<br>';

                //the query results stored in the $results variable are passed to a method that returns the fetched row in an associative array. 
                //The returned array is assigned to a newly initialized variable. 
                //The loop will be executed as long as there are rows returned from the query
                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    //A query is executed retrieving all the criteria which have been previously set for the conference
                    $q = mysql_query("SELECT * FROM conference_rating_criteria cr INNER JOIN rating_criteria r
                            ON cr.Rate_id = r.rate_id
                            WHERE cr.Rate_id = {$row['rate_id']} AND cr.Conference_id = '$confID'");
                            //if the query returns rows
                    if (mysql_num_rows($q) > 0) {
                        //The checkbox list will be populated, checked by default
                        echo "<input type=\"checkbox\" name=\"crit[]\" checked=\"checked\" value=\"{$row['rate_id']}\"> <b>{$row['criteria_type']}:</b> {$row['description']}</input></br>\n";
                    } else {
                        // the checkbox list will be populated, getting as a value the criterion id
                        //and displaying the criteria name beside it. Both values are retrieved from the
                        //associative array $row
                        echo "<input type=\"checkbox\" name=\"crit[]\" value=\"{$row['rate_id']}\"> <b>{$row['criteria_type']}:</b> {$row['description']}</input></br>\n";
                    }
                }
                //after all the criteria are displayed in the checkboxes, an additional checkbox "other" will be created
                //for custom criteria, with a javascript onclick function taking as a parameter its checked status
                //to view the input boxes for a custom criteria
                echo '<input type="checkbox" name="customCrit" onclick="viewCustomFields(this.checked)"> <b>Other</b></input>';
                //a span is created on which the custom criteria textboxes are appended
                echo '&nbsp<span id="cust" style="color:grey;">
                    Name: <input type="text" name="Cname" onblur="isAlphabet(this);"/>
                    Description: <input type="text" name="Cdesc" onblur="isAlphabet(this);"/></span>
                    <br><br>';
                //The label is created to view success/failure messages
                echo '<label id="label3" style="color: red"></label>';
            }

            //condition checks if the submitCrit button was clicked
            if (isset($_POST['submitCrit'])) {
                //Author: Mohamed Tarek
                //condition check if the allow check box was clicked
                if ((isset($_POST['allow']))) {
                    //a query is update in the view_reviews row in conference table.
                    mysql_query("UPDATE conference SET view_review = 1 WHERE conference_id = '$confID' ") or die("error");
                
                   //End Documentation
                }
                //condition checks if there were checkboxes checked in the checkbox list named crit
                if (isset($_POST['crit'])) {
                    //The query deletes all the existing criteria records for this conference as a preparation
                    //for the newly selected ones
                    mysql_query("DELETE FROM conference_rating_criteria WHERE Conference_id = '$confID'");
                    //the values of all the checked checkboxes are stored in an array
                    $aCrit = $_POST['crit'];
                    //a variable is initialized to store the length of the array as an integer
                    $n = count($aCrit);

                    //loops on all the values in the array of checkbox values
                    for ($i = 0; $i < $n; $i++) {
                        //retrieves the value in the array at position i and stores it in a variable
                        $rateid = $aCrit[$i];
                        //executes a query to insert the values of the ids of the checked criteria into the
                        //table with the id of the conference
                        mysql_query("INSERT INTO conference_rating_criteria (Conference_id, Rate_id)
                        VALUES ('$confID', '$rateid')") or die("error");
                    }
                    
//                    echo '<script>'
//                        , 'alert("Your settings have been saved");'
//                        , '</script>';
                    
                } 
                //condition checks if the checkbox for custom criteria is checked
                if (isset($_POST['customCrit'])) {
                    if (($_POST['Cname'] == "") || ($_POST['Cdesc'] == "") || (($_POST['Cname'] == "") && ($_POST['Cdesc'] == ""))) {
                        //Inline javascript calling the alert function to display an error message
                        echo '<script>'
                        , 'alert("Please enter custom criteria or uncheck the box");'
                        , '</script>';
                    } else {
                        //the custom criterion is inserted into the database, retrieving the input
                        //for the query from the dynamically generated textboxes "customName" and "customDesc"
                        mysql_query("INSERT INTO rating_criteria (criteria_type, description)
                            VALUES ('$_POST[Cname]','$_POST[Cdesc]')") or die(mysql_error());
                        //a variable is initialized to contain the id of the newly inserted
                        //criterion using the mysql_insert_id function
                        $cRateID = mysql_insert_id();
                        //a query is executed to insert the the rates that were selected along with the conference
                        //they were selected in, into the database
                        mysql_query("INSERT INTO conference_rating_criteria (Conference_id, Rate_id)
                            VALUES ('$confID', '$cRateID')") or die("error");

                        //Inline javascript calling the alert function to display a success message
                        echo '<script>'
                        , 'alert("Your custom settings have been saved");'
                        , '</script>';
                        
                    }
                }
                
            }



            //closes connection to database
            mysql_close($con);

            //End Documentation
            ?>



            <p>Enter a Start date for review
                :
                <input type="text" name="StartDate" id="startDate" readonly="true" onfocus="javascript: init('startDate');" style="cursor: text;">

            </p>
            <p>Enter a deadline for review
                :
                <input type="text" name="txtDate" id="txtDate" readonly="true" onfocus="javascript: init('txtDate');" style="cursor: text;">
                <br /> <label id="label2"></label>
            </p>
            <p>
                <input type="checkbox" name="allow" />
                Allow reviewers to see other reviews
            </p>
            <br />
            <br />
            <br />
            <input type ="submit" name="submitCrit" value="submit"/><br />
        </form>
        <?php
        if (isset($_POST['txtDate']) && isset($_POST['StartDate'])) { // checking if values passed
            $newdate = $_POST['txtDate'];
            $startnewdate = $_POST['StartDate'];

            if ($newdate != null && $startnewdate != null) {

                // checking if dates are > today and end > start
                $con = mysql_connect('localhost', 'root', ""); //db connection
                if (!$con) {
                    die('connection error');
                } else {
                    //updating review deadline in the database
                    mysql_select_db("mydb", $con);
                    $query = "
                        UPDATE conference
                        SET rev_startDate = '{$startnewdate}', rev_endDate = '{$newdate}'
                        WHERE conference_id = {$confID}
                        ";
                    echo '<script type="text/javascript">'
                    , 'success("deadline set successfuly");'
                    , '</script>';

                    if (!mysql_query($query, $con)) {
                        echo "query not run";
                    }
                    mysql_close($con);
                }
            } else {
                echo '<script type="text/javascript">'
                , 'error("empty or invalid deadline");'
                , '</script>';
            }
        }
        ?>

    </body>
</html>
<head>
     <script type='text/javascript' src="sysJavascript.js"></script>
     <script type="text/javascript" src="calendar.js"></script>
</head>

